<?php
/**
 * team.php
 */
class Team {
	var $id;
	var $team;
	var $memo;	
	var $leagueId;

	function Team($i, $t, $m) {
		$this->id = $i;
		$this->team = $t;
		$this->memo = $m;
	}
	
	function getList() {
		global $database;
	   	$teams = array();
		$result = $database->query("SELECT * FROM teams ORDER BY team");
	    /* Return result array */
	    while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
	    	array_push($teams, $row);
	    }
	    return $teams;		
	}
	
	function set($i=0) {
		global $database;
		if ($i==0) return 0;
		$result = $database->query("SELECT * FROM teams WHERE Id=$i");
        if(!$result || (mysql_numrows($result) < 1)){
	        return -1; //Indicates username failure
	    }
		
		/* Retrieve password from result, strip slashes */
		$dbarray = mysql_fetch_array($result);
		$this->id = $dbarray['Id'];
		$this->team = stripslashes($dbarray['Team']);
		$this->memo = stripslashes($dbarray['Memo']);
		return 1;
	}
	
	function Update() {
		global $database;
		return $database->query("Update teams SET Team='$this->team', Memo='$this->memo' WHERE Id=$this->id;");
	}
	
	function setLeague($leagueId) {
		global $database;
		$result = $database->query("SELECT * FROM league_team WHERE team_id=$this->id AND league_id=$leagueId");
        if(!$result || (mysql_numrows($result) < 1)){
	        return 0;
	    }
	    $dbarray = mysql_fetch_array($result);
	    $this->leagueId = $dbarray["league_id"];
	}
	
	function assignIntoLeague($leagueId) {
		global $database;
		$q="INSERT INTO league_team (league_id, team_id) VALUES ($leagueId, $this->id);";
		return $database->query($q);		
	}
	
	function deleteFromLeague($leagueId) {
		global $database;
		$q="DELETE FROM league_team WHERE league_id=$leagueId AND team_id=$this->id;";
		echo $q . "<br/>";
		return $database->query($q);			
	}
}
?>